<?php
/*
 * @Author: your name
 * @Date: 2021-11-11 16:46:31
 * @LastEditTime: 2022-01-17 17:43:55
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \property\app\Http\Middleware\CheckSession.php
 */

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App\Http\Controllers\redis\RedisController as Redis;
class CheckCrmToken
{
    

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
     
        $redisKey   =   CRM_LOGIN_REDIS_KEY;
        $headerKey  =   CRM_LOGIN_HEADER_KEY;
      
        // print_r($_SERVER);
        if(isset($_SERVER[$headerKey]) && $_SERVER[$headerKey]   && strlen( $_SERVER[$headerKey] ) == 32 ){
            $key = $redisKey.$_SERVER[$headerKey];
           if( Redis::existSKey( $key ,['RedisDB'=>11]) ){
               Redis::expire($key,7*24*3600,['RedisDB'=>11]);
           }else{
               return  toJson(-1,'登录已过期') ;
           }


        }else{
            return   abort(400);
        }

        return $next($request);
    }
}
